package com.timanetworks.mailbox.common.busi;

import com.timanetworks.mailbox.common.http.CommonHttpResponse;
import com.timanetworks.mailbox.common.http.HttpConnectionService;
import com.timanetworks.mailbox.common.response.Message;
import org.apache.http.Header;
import org.apache.http.message.BasicHeader;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("http-test")
public class HttpController {

    @Autowired
    private HttpConnectionService connection;


    @PostMapping("login")
    public Message login(){
        String message = "{\"auth\":{\"identity\":{\"methods\":[\"password\"],\"password\":{\"user\":{\"name\":\"admin\",\"domain\":{\"name\":\"Default\"},\"password\":\"tima123\"}}}}}";
        try{
            CommonHttpResponse response = connection.post("172.17.223.21", 35357, "/v3/auth/tokens",  message);
            return Message.ok().data(response.getBody());
        }catch (Exception e){
            e.printStackTrace();
            return Message.error(701).message("服务异常！");
        }
    }

    @GetMapping("servers")
    public Message queryServers(){
        try{
            Header header = new BasicHeader("X-Auth-Token", "6bb32048daff4da0bb9f41ea4db4a0f4");
            CommonHttpResponse response = connection.get("172.17.223.21", 8774, "/v2.1/servers/detail", header);
            return Message.ok().data(response.getBody());
        }catch (Exception e){
            e.printStackTrace();
            return Message.error(701).message("服务异常！");
        }
    }
}
